package com.huatai.vba.execute.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.huatai.vba.common.utils.ListUtils;
import com.huatai.vba.dal.jdbc.DBUtils;
import com.huatai.vba.properties.Dbconfig;
import com.huatai.vba.service.VbaDictionaryService;
import com.huatai.vba.service.VbaExtendsService;

public abstract class AbstractIcbcActionExecutor extends AbstractExcutor implements ActionExecutor {

	protected static final Logger logger = LoggerFactory.getLogger(ActionExecutor.class);

	@Autowired
	protected VbaDictionaryService vbaDictionaryService;

	@Autowired
	protected VbaExtendsService vbaExtendsService;

	protected static final String baseFilePath = "c:/ICBS_cus/";

	protected static final String user = "trade_zc";

	// 恒生基金代码
	protected static final String[] strOne = new String[] { "777101", "777102", "777103", "980019", "980020", "980023",
			"980024", "980026", "980027", "980028", "980029", "980030", "980031", "980032", "980018", "980025",
			"980036", "990019", "990020", "990016", "980039", "980040", "980041", "980044", "980037", "980038",
			"990022", "980049", "980055", "980056", "980057", "990043", "980100", "980101", "980014", "990061",
			"990079", "990084", "990087", "990089", "980094", "980095", "980096" };

	// 工行组合代码
	protected static final String[] strTwo = new String[] { "777101", "777102", "777103", "980019", "980020", "980023",
			"980024", "980026", "980027", "980028", "980029", "980030", "980031", "980032", "980018", "980025",
			"980036", "000095", "000096", "990016", "980039", "980040", "980041", "000089", "980037", "980038",
			"990022", "980049", "980055", "980056", "980057", "130174", "980100", "980101", "000088", "000273",
			"154818", "152593", "152538", "152540", "980094", "980095", "980096" };

	// 上海股东代码
	protected static final String[] strThree = new String[] { "B881498268", "B881498268", "B881498268", "B880777978",
			"B881031561", "B880635710", "B880635710", "B880777986", "B880777994", "B880778005", "B880777960",
			"B880777986", "B880777986", "B880777986", "0", "0", "0", "0", "B881031561", "0", "B881482924", "0", "0",
			"0", "0", "0", "0", "B881031561", "B881031561", "B881031561", "B881031561", "B881307093", "B881031561",
			"B881031561", "0", };

	// 深圳股东代码
	protected static final String[] strFour = new String[] {};

	// 恒生基金代码（信诚）
	protected static final String[] strFive = new String[] { "980069", "980081", "980082", "980075", "980076",
			"980077" };

	// 中融组合代码
	protected static final String[] strSix = new String[] { "86", "108", "87", "980075", "980076", "980077", };

	// 公用股东帐户代码
	protected static final String[] strSeven = new String[] { "980055", "980056", "980057", "980100", "980101",
			"980094", "980095", "980096", "980012", "980011", "777101", "777102", "777103", "980023", "980024",
			"980020", "980039", "980040", "980041", "980014", "980044", "980122", "980123", "980063" };

	protected String Trim(Object str) {
		return (str + "").trim();
	}

	protected String CStr(Object str) {
		return str + "";
	}

	protected String Left(String str, Integer num) {
		return str.substring(0, num);
	}

	protected String Right(String str, Integer num) {
		return str.substring(str.length() - num, str.length());
	}

	protected String Mid(String str, Integer num, Integer size) {
		return str.substring(num - 1, num + size - 1);
	}

	protected String getCombId(String tmp_CJBH) throws Exception {
		return getCombId(tmp_CJBH, "", "");
	}

	protected String getCombId(String tmp_CJBH, String VC_report_direction, String c_market_no) throws Exception {
		StringBuffer strSql = new StringBuffer("select combi_id from hsfundid where cjbh='");
		strSql.append(tmp_CJBH).append("'");
		if (StringUtils.isNotBlank(VC_report_direction)) {
			strSql.append(" and direction = '").append(VC_report_direction).append("' ");
			strSql.append(" and market_no = '").append(c_market_no).append("' ");
		}
		List<Map<String, Object>> result = vbaExtendsService.querySql(strSql.toString());
		if (result.size() == 1) {
			return result.get(0).get("COMBI_ID") + "";
		}
		return null;
	}

	protected String getRepoCode(String istrMarket) throws Exception {
		StringBuffer sql = new StringBuffer("select vc_report_code from tstockinfo t");
		if ("SH".equals(istrMarket))
			sql.append(" where c_market_no = 1 ");
		if ("SZ".equals(istrMarket))
			sql.append(" where c_market_no = 2 ");
		sql.append(" and c_stock_type = '8' ");
		List<Map<String, Object>> list = DBUtils.executeQuery(Dbconfig.TRADEDB, user,
				Dbconfig.getPasswordByUserId(user), sql.toString());
		StringBuffer strTmp = new StringBuffer();
		for (Map<String, Object> map : list) {
			String str = (String) map.get("vc_report_code".toUpperCase());
			strTmp.append(",").append(str);
		}
		return strTmp.toString().substring(1);
	}

	protected String getHSLZ(String tmp_CJBH, String VC_report_direction, String c_market_no) throws Exception {
		StringBuffer strSql = new StringBuffer("select investtype from hsfundid where cjbh='" + tmp_CJBH + "'");
		if (StringUtils.isNotBlank(VC_report_direction)) {
			strSql.append(" and direction = '").append(VC_report_direction).append("' ");
			strSql.append(" and market_no = '").append(c_market_no).append("' ");
		}
		List<Map<String, Object>> result = vbaExtendsService.querySql(strSql.toString());
		if (result.size() == 1) {
			return result.get(0).get("INVESTTYPE") + "";
		}
		return null;
	}

	// 从恒生交易系统里取深交所回购信息，判断该代码是否为回购代码
	protected boolean isSjshg(String istrZqdm) throws Exception {
		StringBuffer sql = new StringBuffer("select 1 from tstockinfo t ");
		sql.append(" where c_market_no = 2 ");
		sql.append(" and c_stock_type = '8' ");
		sql.append("  and vc_report_code = '" + istrZqdm + "'");
		List<Map<String, Object>> list = DBUtils.executeQuery(Dbconfig.TRADEDB, user,
				Dbconfig.getPasswordByUserId(user), sql.toString());
		return ListUtils.isNotEmpty(list);
	}

	protected String convertDateFormat(String tmp_date) {
		return convertDateFormat(tmp_date, false);
	}

	protected String convertDateFormat(String tmp_date, boolean strTF) {
		String[] str = tmp_date.split("-");
		if (strTF) {
			return str[0] + str[1] + str[2];
		} else {
			return str[1] + "/" + str[2] + "/" + str[0];
		}
	}

	protected String getICBCCombi(String strFundid) {
		for (int i = 0; i < strOne.length; i++) {
			if (strOne[i].equals(strFundid)) {
				return strTwo[i];
			}
		}
		return null;
	}

	protected String convertSql(String[] args) {
		StringBuffer sb = new StringBuffer();
		if (args == null || args.length == 0)
			return null;
		for (String str : args) {
			sb.append(",'").append(str).append("'");
		}
		return sb.toString().substring(1);
	}

	protected String convertStrDateFormat(String tmp_date) {
		return tmp_date.substring(0, 4) + "-" + tmp_date.substring(4, 6) + "-" + tmp_date.substring(6, 8);
	}

}
